---
title: "WSL笔记"
categories:
- windows
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#windows-subsystem-for-linux-faq-ubuntu-18.04">Windows Subsystem for Linux FAQ (Ubuntu 18.04)</a>
<ul>
<li><a href="#reset-password-重置密码">Reset Password 重置密码</a></li>
<li><a href="#restart-wsl-重启">Restart WSL 重启</a></li>
<li><a href="#ssh-service">SSH Service</a></li>
<li><a href="#org32b608a">解决误删除wsl目录后无法再次安装</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-org804093f">
<h2 id="windows-subsystem-for-linux-faq-ubuntu-18.04"><a id="org804093f"></a>Windows Subsystem for Linux FAQ (Ubuntu 18.04)</h2>
<div class="outline-text-2" id="text-windows-subsystem-for-linux-faq-ubuntu-18.04">
</div>
<div class="outline-3" id="outline-container-org1898f32">
<h3 id="reset-password-重置密码"><a id="org1898f32"></a>Reset Password 重置密码</h3>
<div class="outline-text-3" id="text-reset-password-重置密码">
<blockquote>
<p>
<a href="https://askubuntu.com/questions/772050/reset-the-password-in-ubuntu-linux-bash-in-windows">https://askubuntu.com/questions/772050/reset-the-password-in-ubuntu-linux-bash-in-windows</a>
</p>
</blockquote>
<ol class="org-ol">
<li>Close Bash on Ubuntu if it is running (or the next command will
fail).</li>
<li>Run cmd.exe with Administror user</li>
<li>=ubuntu config –default-user root=这条命令更改默认的登录用户为root。</li>
<li>再次打开bash，登录用户已经是root，然后=passwd user=。如果不是root登录的，上一步之后也许需要重启wsl服务。</li>
<li>密码重置之后，再用=ubuntu config –default-user user=，改回原来的默认账号。</li>
</ol>
<p>
WSL Ubuntu 18.04需要改用命令=ubuntu1804=。
</p>
<p>
如果重启wsl之后，仍然无法使用root账户登录，则尝试直接修改文件系统。WSL的文件存放在=%localappdata%\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs\=，直接修改文件系统能解决很多问题，而不止是密码。
</p>
<p>
早期的WIN
10版本通过=lxrun=命令管理WSL(Beta)。从应用商店安装的WSL则使用=ubuntu=命令。
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org6b55c1e">
<h3 id="restart-wsl-重启"><a id="org6b55c1e"></a>Restart WSL 重启</h3>
<div class="outline-text-3" id="text-restart-wsl-重启">
<p>
重启=LxssManager=服务，在服务管理器或进程管理器中。
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org7749d72">
<h3 id="ssh-service"><a id="org7749d72"></a>SSH Service</h3>
<div class="outline-text-3" id="text-ssh-service">
<blockquote>
<p>
<a href="https://github.com/Microsoft/WSL/issues/612">https://github.com/Microsoft/WSL/issues/612</a>
<a href="https://blog.saintic.com/blog/254.html">https://blog.saintic.com/blog/254.html</a>
</p>
</blockquote>
<p>
wsl默认打开的命令行和Bash还是有所差别的，例如翻页键。还有的不兼容的地方，显示都乱了，例如Emacs。还有颜色模式，深蓝色根本看不清。
</p>
<p>
一个解决方案就是使用其他的Shell。Cygwin、XTerm、以前试过，还是有不兼容的。Mintty有专门的For
wsl版本，现在不知道怎么样了。
</p>
<p>
还有一种方案，使用ssh连接wsl，这种兼容性应该完美的，是我目前正在使用的方案。
</p>
<ol class="org-ol">
<li>安装sshd，wsl下安装openssh-server：=apt install openssh-server=</li>
<li>配置sshd.</li>
</ol>
<pre class="example">
Port 2222 # 监听端口需要改成与windows其他服务不冲突的
PasswordAuthentication yes # 开户密码授权
</pre>
<p>
然后测试，通过=sudo service ssh start=看看服务能否启动成功。WSL中还没有systemctl。如果Windows防火墙弹出，选择“允许”。在Windows上使用ssh客户端连接=ssh user@localhost -p 2222=。
</p>
<ol class="org-ol">
<li>以下为设置一键启动的方法，为了提升作用体验。如果每次开机都手工进行以上操作就太麻烦了，通过脚本解决所有问题。</li>
<li>The Key：启动sshd的脚本</li>
</ol>
<pre class="example">
bash.exe -c "sudo service ssh start"
</pre>
<p>
为了使这个脚本能自动执行而不用输入密码，需要给当前用户配置sudo。通过编辑``/etc/sudoers=来实现，使用命令=visudo```，将如下行添加到*适当*的地方。由于sudoers是有顺序的，要注意下行的位置，不能被覆盖了。
</p>
<pre class="example">
YOUR_USER     ALL=(ALL) NOPASSWD: /usr/sbin/service
</pre>
<p>
如果sudoers配置正确，我们自己手工输入=sudo servcice ssh start=就无须密码了，在脚本中也就不需要了。
</p>
<ol class="org-ol">
<li>开机启动</li>
</ol>
<p>
如果想开机启动wsl的sshd，则把脚本加入到Windows计划任务中。Windows Task
Scheduler, create new basic task with following settings: * Run: When
the computer starts * Action: Start a program * Program:
c:\Windows\system32\bash.exe * Arguments: -c "sudo service ssh start"
</p>
<p>
Test it by clicking on the Task Scheduler LIbrary, selecting your new
task and clicking Run on the right-hand side. A command prompt window
should open and remain open.
</p>
<p>
If you want to hide the window, I think you can tick the Hidden box in
the scheduled task properties.
</p>
<ol class="org-ol">
<li>一键启动</li>
</ol>
<p>
Create a vb script
</p>
<div class="org-src-container">
<pre class="src src-vbs">sub start_wsl()
    Set ws = WScript.CreateObject("WScript.Shell")
    cmd = "C:\Windows\System32\bash.exe -c ""sudo service ssh start"""
    '运行命令不显示cmd窗口
    ws.Run cmd, 0, false    
    Set ws = Nothing    
end sub

sub start_tty(cmd)
    dim objShell
	Set objShell = WScript.CreateObject("WScript.Shell")
	objShell.Run(cmd)
	Set objShell = Nothing
end sub

start_wsl
start_tty """C:\Program Files\Git\git-bash.exe"""
WScript.quit
</pre>
</div>
<p>
在这个脚本右键，选择发送到桌面快捷方式。也就是给它创建了个快捷方式。为了美观，右键这个快捷方式，为它选择一个图标。
</p>
<p>
把快捷方式复制到开始菜单所在目录，然后在开始菜单中找到它，选择固定到开始屏幕。
</p>
<p>
WIN10开始菜单的路径为=%APPDATA%\Microsoft\Windows\Start Menu\Programs=
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org32b608a">
<h3 id="org32b608a">解决误删除wsl目录后无法再次安装</h3>
<div class="outline-text-3" id="text-org32b608a">
<blockquote>
<p>
<a href="https://www.reddit.com/r/bashonubuntuonwindows/comments/aqeu24/i_accidentally_deleted_my_installed_wsl_folder/">https://www.reddit.com/r/bashonubuntuonwindows/comments/aqeu24/i_accidentally_deleted_my_installed_wsl_folder/</a>
</p>
</blockquote>
<p>
使用 wslconfig 命令
</p>
<p>
防墙：
</p>
<blockquote>
<p>
What version of Windows are you using? Do you have wslconfig.exe?
</p>
<p>
permalinkembedsavereportreply
</p>
<p>
[–]tenbuckscye[S] 1 point 4 hours ago 
</p>
<p>
The windows version is Version 1809 (OS Build 17763.253). Yes I do.
</p>
<p>
permalinkembedsaveparentreportreply
</p>
<p>
[–]benheliozWSL Developer 2 points 3 hours ago 
</p>
<p>
What is the output of "wslconfig /list"
</p>
<p>
You'll want to unregister the distro you deleted with "wslconfig /unregister"
</p>
<p>
permalinkembedsaveparentreportreply
</p>
<p>
[–]tenbuckscye[S] 1 point 3 hours ago 
</p>
<p>
That is done, I ran both commands and it said the Debian distro is uninstalled. Would that mean I can reinstall now?
</p>
<p>
permalinkembedsaveparentreportreply
</p>
<p>
[–]benheliozWSL Developer 2 points 3 hours ago 
</p>
<p>
Yep. Launch the Debian tile again and that should be it.
</p>
<p>
permalinkembedsaveparentreportreply
</p>
<p>
[–]tenbuckscye[S] 2 points 3 hours ago 
</p>
<p>
Awesome. Thanks so much!
</p>
<p>
permalinkembedsaveparentreportreply
</p>
<p>
[–]angellus 2 points 4 hours ago 
</p>
<p>
lxrun is for the legacy WSL install, it has been deprecated for almost a year now. You should not be using it anymore anyways.
</p>
<p>
<a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10">https://docs.microsoft.com/en-us/windows/wsl/install-win10</a>
</p>
<p>
<a href="https://docs.microsoft.com/en-us/windows/wsl/wsl-config">https://docs.microsoft.com/en-us/windows/wsl/wsl-config</a>
</p>
<p>
permalinkembedsavereportreply
</p>
<p>
[–]tenbuckscye[S] 1 point 4 hours ago 
</p>
<p>
That's what I used to download my WSL. I used <a href="https://superuser.com/questions/1271682/is-there-a-way-of-installing-windows-subsystem-for-linux-on-win10-v1709-withou">https://superuser.com/questions/1271682/is-there-a-way-of-installing-windows-subsystem-for-linux-on-win10-v1709-withou</a> more specifically because I do not have Windows store access on my computer.
</p>
<p>
permalinkembedsaveparentreportreply
</p>
</blockquote>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
